package laevpomm;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Pomm
 */
public class Pomm extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final List<BlockingQueue<String>> msgQueues = new ArrayList<BlockingQueue<String>>();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/plain");
		int id = Integer.parseInt(request.getParameter("id"));
		try {
			BlockingQueue<String> q = msgQueues.get(id);
			response.getWriter().print(q.take());
		} catch (InterruptedException e) {
			throw new RuntimeException(e);
		}
		//String param2 = request.getParameter("");
		// TODO Auto-generated method stub
	}
	
	public Statement getConnection() throws ServletException{
		Connection con = null;  
		Statement stmt = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con =DriverManager.getConnection 
					("jdbc:mysql://192.168.10.59:3306/example","root","root");
			stmt = con.createStatement();
			return stmt;
		} catch (SQLException e) {
			throw new ServletException("Servlet Could not display records.", e);
		} catch (ClassNotFoundException e) {
			throw new ServletException("JDBC Driver not found.", e);
		}
	}
	
	// ships[0][0][0] on esimese laeva esimese kasti esimene muutuja (ehk rida, ehk t2ht)
	// ships[0][0][1] on esimese laeva esimese kasti teine muutuja (ehk veerg, ehk number)
	//siia peaks tulema meetod kuhu peitub laevade asukohad.

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/plain");
		
		System.out.println(request.getParameterMap());
		
		String action = request.getParameter("action"); // v6imalikud v22rtused on: register, setup, shoot, getEnemyShot
		String playerId = request.getParameter("id"); 
		
		if (action != null && action.equals("register")) {
			int id = msgQueues.size();
			response.getWriter().print(id); // send ID
			msgQueues.add(new ArrayBlockingQueue<String>(100));
		} else if (action != null && action.equals("shoot")){
			
		} else if (action != null && action.equals("setup") && playerId != null){
			System.out.println("Setup! " + "playerId: " + playerId);
			String ship1, ship2, ship3, ship4, ship5;
			ship1 = request.getParameter("ship1");
			ship2 = request.getParameter("ship2");
			ship3 = request.getParameter("ship3");
			ship4 = request.getParameter("ship4");
			ship5 = request.getParameter("ship5");
			System.out.println("Ship1: " + ship1 + " Ship2: " + ship2 + " Ship3: " + ship3 + " Ship4: " + ship4 + " Ship5: " + ship5);
		} else if (action != null && action.equals("shoot") && playerId != null){
			System.out.println("Shoot!");
		} else if (action != null && action.equals("getEnemyShot")){
			System.out.println("EnemyShot!");
		} else {
			int id = Integer.parseInt(request.getParameter("id"));
			
			try {
				// post in all queues
				for (int i=0; i < msgQueues.size(); i++) {
					if (i != id)
					{
						msgQueues.get(i).put(request.getParameter("msg"));
					}
				}
				response.getWriter().print("OK");
			} catch (InterruptedException e) {
				throw new RuntimeException(e); 
			}
		}
	}
}
